/*
 * Created on May 29, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.novix.portal.framework.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.View;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.log4j.Logger;

import com.novix.portal.framework.dao.IWebContactDAO;
import com.novix.portal.framework.exceptions.ApplicationException;
import com.novix.portal.framework.model.impl.WebContact;

/**
 * @author Dev
 * 
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code
 * Templates
 */
public class WebContactDAO extends BaseDAO implements IWebContactDAO {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(WebOrganizationDAO.class);

    private static final boolean IS_DEBUG = logger.isDebugEnabled();

    /**
     * called from cache loader
     * @bug object removed
     */
    public List selectAllWebContacts(Session session, String serverName, String iePath) {

        List list = null;
        try {

            Database notesDatabase = session.getDatabase(serverName, iePath);
            if (!notesDatabase.isOpen())
                notesDatabase.open();
            View view = notesDatabase.getView("webcontactphoto");

            Document doc = view.getFirstDocument();

            list = new ArrayList();
            while (doc != null) {
                WebContact webContact = new WebContact();
                webContact.setCompanyName(doc.getItemValueString("CompanyName"));
                webContact.setLink(doc.getItemValueString("WebSite"));
                webContact.setCaptives(doc.getItemValue("captives"));
                webContact.setFullName(doc.getItemValueString("fullname"));
                webContact.setLastName(doc.getItemValueString("LastName"));
                webContact.setInternetAddress(doc.getItemValueString("InternetAddress"));
                webContact.setJobTitle(doc.getItemValueString("JobTitle"));
                webContact.setSpouseName(doc.getItemValueString("Spouse"));
                ArrayList catList = getCategories(doc);
                webContact.setCategories(catList);
                webContact.setOfficePhoneNumber(doc.getItemValueString("OfficePhoneNumber"));
                webContact.setOfficeFaxNumber(doc.getItemValueString("OfficeFAXPhoneNumber"));
                webContact.setCompanyId(doc.getItemValueString("IENCompanyDocumentUniqueID"));
                setWebSiteLinkForContacts(doc, webContact);
                webContact.setId(doc.getUniversalID());
                setAttachmentName(session, serverName, doc, webContact);
                if (IS_DEBUG)
                    logger.debug(doc.getUniversalID());
                list.add(webContact);
                doc = view.getNextDocument(doc);
            }

        } catch (NotesException e) {
            String errorMessage = "Error in selectAllWebContacts ...";
            logger.error(errorMessage + " " + e.id + " " + e.text, e);
            throw new ApplicationException(errorMessage);
        }
        BeanComparator comp = new BeanComparator("companyName");
        Collections.sort(list, comp);
        return list;

    }

    /*
     * (non-Javadoc)
     * 
     * @see com.novix.portal.framework.dao.IWebContactDAO#selectAllWebContacts(java.lang.String, java.lang.String,
     *      java.lang.String, java.lang.String, java.lang.String)
     */
    public List selectAllWebContactsPicsOrNone(String serverConnectionString, String token, String captiveName,
            String serverName, String ienaddressesPath) {

        return super.selectAllWebContactsPicsOrNo(serverConnectionString, token, captiveName, serverName,
                ienaddressesPath, true);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.novix.portal.framework.dao.IWebContactDAO#selectAllWebContacts(java.lang.String, java.lang.String,
     *      java.lang.String, java.lang.String, java.lang.String)
     */
    public List selectAllWebContacts(String serverConnectionString, String token, String captiveName,
            String serverName, String ienaddressesPath) {

        return super.selectAllWebContacts(serverConnectionString, token, captiveName, serverName, ienaddressesPath);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.novix.portal.framework.dao.IWebContactDAO#selectAllWebContactsPicsOrNone(lotus.domino.Session,
     *      java.lang.String, java.lang.String)
     */
    public List selectAllWebContactsPicsOrNone(Session session, String serverName, String iePath) {
        return super.selectAllWebContactsPicsOrNo(session, serverName, iePath);
    }
}
